package com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.teacher.entity.SmsTeacherInfo;
import com.xyht.sca_s.student_manage_system.modules.teacher.mapper.SmsTeacherInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.entity.SmsTeachersDemeanor;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.entity.req.SmsTeachersDemeanorAddReq;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.entity.req.SmsTeachersDemeanorDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.entity.resp.SmsTeachersDemeanorDetailResp;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.mapper.SmsTeachersDemeanorMapper;
import com.xyht.sca_s.student_manage_system.modules.teachersDemeanor.service.SmsTeachersDemeanorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 教师风采配置表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-09-03
 */
@Service
public class SmsTeachersDemeanorServiceImpl extends ServiceImpl<SmsTeachersDemeanorMapper, SmsTeachersDemeanor> implements SmsTeachersDemeanorService {

    @Resource
    private SmsTeachersDemeanorMapper smsTeachersDemeanorMapper;
    @Resource
    private SmsTeacherInfoMapper smsTeacherInfoMapper;

    @Override
    public ResponseResult addTeachersDemeanor(SmsTeachersDemeanorAddReq addReq) {
        if(addReq.getTchIdList() == null || addReq.getTchIdList().size() < 1){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<String> teacherIdList = addReq.getTchIdList();
        for (String teacherId : teacherIdList) {
            SmsTeachersDemeanor smsTeachersDemeanor = new SmsTeachersDemeanor();
            smsTeachersDemeanor.setTeacherId(teacherId);
            smsTeachersDemeanorMapper.insert(smsTeachersDemeanor);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult getTeachersDemeanor(String id) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsTeachersDemeanor teachersDemeanor = smsTeachersDemeanorMapper.selectById(id);
        if(teachersDemeanor == null){
            return CommonResult.failed(CommonCodeEnum.TEACHERS_DEMEANOR_NOT_EXIST);
        }
        String teacherId = teachersDemeanor.getTeacherId();
        if(isNullOrEmpty(teacherId)){
            return CommonResult.failed(CommonCodeEnum.TEACHER_NOT_EXIST);
        }
        SmsTeacherInfo smsTeacherInfo = smsTeacherInfoMapper.selectById(teacherId);
        if(smsTeacherInfo == null){
            return CommonResult.failed(CommonCodeEnum.TEACHER_NOT_EXIST);
        }
        SmsTeachersDemeanorDetailResp teachersDemeanorDetailResp = new SmsTeachersDemeanorDetailResp();
        teachersDemeanorDetailResp.setId(teachersDemeanor.getId());
        teachersDemeanorDetailResp.setTeacherId(teacherId);
        teachersDemeanorDetailResp.setTeacherPic(smsTeacherInfo.getTchPic());//头像
        teachersDemeanorDetailResp.setTeacherName(smsTeacherInfo.getTchName());//姓名
        teachersDemeanorDetailResp.setTeacherTitle(smsTeacherInfo.getTchTitle());//教师职级(职称)
        teachersDemeanorDetailResp.setTeacherPost(smsTeacherInfo.getTchPost());//教师职位(职务)
        return CommonResult.success(teachersDemeanorDetailResp);
    }

    @Override
    public ResponseResult deletedTeachersDemeanor(SmsTeachersDemeanorDeletedReq deletedReq) {
        if(deletedReq.getIdList() == null || deletedReq.getIdList().size() < 1){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<String> idList = deletedReq.getIdList();
        for (String id : idList) {
            SmsTeachersDemeanor smsTeachersDemeanor = smsTeachersDemeanorMapper.selectById(id);
            if(smsTeachersDemeanor == null){
                return CommonResult.failed(CommonCodeEnum.TEACHERS_DEMEANOR_NOT_EXIST);
            }
        }
        smsTeachersDemeanorMapper.deleteBatchIds(idList);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getTeachersDemeanorList(Integer pageSize, Integer pageNum, String teacherName) {
        Page<SmsTeachersDemeanor> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsTeachersDemeanor> queryWrapper = new LambdaQueryWrapper<>();
        if(!isNullOrEmpty(teacherName)){
            List<String> teacherIds = smsTeacherInfoMapper.selectList(new QueryWrapper<SmsTeacherInfo>()
                    .lambda()
                    .like(SmsTeacherInfo::getTchName, teacherName))
                    .stream()
                    .map(SmsTeacherInfo::getId).collect(Collectors.toList());
            queryWrapper.in(SmsTeachersDemeanor::getTeacherId,teacherIds);
        }
        smsTeachersDemeanorMapper.selectPage(page,queryWrapper);
        List<SmsTeachersDemeanorDetailResp> list = new ArrayList<>();
        list = page.getRecords()
                .stream()
                .map(smsTeachersDemeanor -> {
                    SmsTeachersDemeanorDetailResp detailResp = new SmsTeachersDemeanorDetailResp();
                    SmsTeacherInfo smsTeacherInfo = smsTeacherInfoMapper.selectById(smsTeachersDemeanor.getTeacherId());
                    detailResp.setId(smsTeachersDemeanor.getId());
                    detailResp.setTeacherId(smsTeachersDemeanor.getTeacherId());
                    if(smsTeacherInfo != null){
                        detailResp.setTeacherPic(smsTeacherInfo.getTchPic());//头像
                        detailResp.setTeacherName(smsTeacherInfo.getTchName());//姓名
                        detailResp.setTeacherTitle(smsTeacherInfo.getTchTitle());//教师职级(职称)
                        detailResp.setTeacherPost(smsTeacherInfo.getTchPost());//教师职位(职务)
                    }
                    return detailResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }
}
